using Catalog.Application.Categorys.Interfaces;
using Catalog.Application.Categorys.Queries;
using Catalog.Application.Contracts.DTOs;
using MediatR;

namespace Catalog.Application.Categorys.QueryHandlers;

public class GetCategoryByIdQueryHandler : IRequestHandler<GetCategoryByIdQuery, CategoryProfileDto>
{
    private readonly ICategoryReadService _categoryReadService;

    public GetCategoryByIdQueryHandler(ICategoryReadService categoryReadService)
    {
        _categoryReadService = categoryReadService;
    }
    public async Task<CategoryProfileDto> Handle(GetCategoryByIdQuery request, CancellationToken cancellationToken)
    {
        var category = await _categoryReadService.GetByIdAsync(request.id);
        if (category == null)
        {
            throw new KeyNotFoundException($"Category with ID {request.id} not found.");
        }

        return new CategoryProfileDto
        {
            Id = category.Id,
            Name = category.Name,
            Description = category.Description
        };
    }
}